package com.bdsoft.actions.RoleManage.Service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.bdsoft.actions.RoleManage.DTO.RoleDTO;
import com.bdsoft.actions.RoleManage.Dao.RoleMapper;
import com.bdsoft.actions.RoleManage.Service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public PageInfo<Map<String,Object>> getRoleList(int pageNum, int pageSize, String id, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,Object>> resList = roleMapper.selectRoleByParams(id, name);
        return new PageInfo<>(resList);
    }

    @Override
    public Map<String, Object> saveOrUpdateRole(RoleDTO roleDTO) {
        if (StrUtil.isBlank(roleDTO.getId())) {
            roleDTO.setId(UUID.randomUUID().toString());
            int result = roleMapper.addRole(roleDTO);
            return result > 0 ? MapUtil.<String,Object>builder()
                    .put("code", 200)
                    .put("data", "添加角色成功")
                    .build()
                : MapUtil.<String,Object>builder()
                    .put("code", 500)
                    .put("data", "添加角色失败")
                    .build();
        } else {
            int result = roleMapper.updateRoleById(roleDTO);
            return result > 0 ? MapUtil.<String,Object>builder()
                    .put("code", 200)
                    .put("data", "修改角色成功")
                    .build()
                : MapUtil.<String,Object>builder()
                    .put("code", 500)
                    .put("data", "修改角色失败")
                    .build();
        }
    }

    @Override
    public Map<String, Object> deleteRoleById(String id) {
        int result = roleMapper.deleteRoleById(id);
        return result > 0 ? MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", "删除角色成功")
                .build()
            : MapUtil.<String,Object>builder()
                .put("code", 500)
                .put("data", "删除角色失败(或者找不到角色)")
                .build();
    }

    @Override
    public Map<String, Object> getMenuList(String roleId) {
        return MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", roleMapper.selectMenuByRoleId(roleId))
                .build();
    }
} 